import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from python_ai.common.xcommon import sep

df = pd.DataFrame([['green', 'a'],
                   ['green', 'b'],
                   ['blue', 'a'],
                   ['blue', 'b'],
                   ['green', 'a']])
print(df)

sep('onehot')
# get_dummies is just onehot encoder
df1 = pd.get_dummies(df)
print(type(df1))
print(df1)

sep('onehot on col0')
df2 = pd.get_dummies(df[0])
print(type(df2))
print(df2)

sep('reverse onehot on col0')
# https://stackoverflow.com/questions/50607740/reverse-a-get-dummies-encoding-in-pandas
sep('df2 == 1')
print(df2 == 1)
sep('df2[df2 == 1]')
print(df2[df2 == 1])
sep('df2[df2 == 1].stack()')
stacked = df2[df2 == 1].stack()
print(len(stacked.index))
print(stacked.index)
# print(len(stacked.columns))  # AttributeError: 'Series' object has no attribute 'columns'
print(stacked)
sep('reset index')
r = stacked.reset_index()
print(type(r))
print(r)
sep('drop')
r = r.drop(['level_0', 0], axis=1)
print(type(r))
print(r)
sep('rename')
r.rename(columns={'level_1': 'color'}, inplace=True)
print(type(r))
print(r)
